using System;
using System.Collections.Generic;
using System.Windows.Forms;
using LiteTools.domains.db;
using Sunny.UI;

namespace LiteTools.forms
{
    public partial class SpecificConfigForm : UIForm
    {
        public SpecificConfigForm()
        {
            InitializeComponent();
            InitComboxes();
            RefreshTagInfoView();
        }

        private void RefreshTagInfoView()
        {
            var tagInfos = TagInfoHelper.GetTagInfos();
            if (tagInfos == null)
            {
                return;
            }

            var tlvTagFamilies = TlvTagFamilyHelper.GetTlvTagFamily();
            var tagFamilies = new Dictionary<string, TlvTagFamilyHelper.TlvTagFamily>();
            if (tlvTagFamilies != null)
            {
                foreach (var tlvTagFamily in tlvTagFamilies)
                {
                    tagFamilies.Add(tlvTagFamily.ttf_id, tlvTagFamily);
                }
            }

            // 创建一个 `BindingSource` 对象
            BindingSource bindingSource = new BindingSource();

            // 将 `BindingSource` 对象绑定到 `DataGridView` 对象
            tag_infos_view.DataSource = bindingSource;

            foreach (var tagInfo in tagInfos)
            {
                bindingSource.Add(new
                {
                    Tag值 = tagInfo.tag_value, Tag所属族 = tagFamilies[tagInfo.tag_family].ttf_name,
                    Tag描述 = tagInfo.tag_desc, 类型 = tagInfo.tag_type == 0.ToString() ? "简单型" : "结构性",
                    FAMILY_ID = tagFamilies[tagInfo.tag_family].ttf_id, ID = tagInfo.tag_id
                });
            }

            tag_infos_view.Columns[0].Width = 90;
            tag_infos_view.Columns[1].Width = 150;
            tag_infos_view.Columns[2].Width = 380;
            tag_infos_view.Columns[3].Width = 95;
            tag_infos_view.Columns[4].Width = 25;
            tag_infos_view.Columns[5].Width = 25;
            tag_infos_view.Columns[4].Visible = false;
            tag_infos_view.Columns[5].Visible = false;
            tag_infos_view.RowHeadersVisible = false;
            // 刷新 `DataGridView` 对象
            tag_infos_view.Refresh();
        }

        private void InitComboxes()
        {
            function_type.SelectedIndex = 0;
        }

        private void add_config_Click(object sender, EventArgs e)
        {
            if (function_type.SelectedIndex == 0)
            {
                var tagModelViewer = new TagModelViewer();
                tagModelViewer.ShowDialog();
                ShowModels();
            }
            else
            {
                return;
            }
        }

        private void ShowModels()
        {
        }

        private void remove_config_Click(object sender, EventArgs e)
        {
            throw new System.NotImplementedException();
        }

        private void edit_config_Click(object sender, EventArgs e)
        {
            throw new System.NotImplementedException();
        }
    }
}